package com.ggh.jinjilive.ui.mall.shop;

import androidx.lifecycle.MutableLiveData;

import com.ggh.jinjilive.mobile.ResultEntity;
import com.ggh.jinjilive.mobile.mall.MallGoods;
import com.ggh.jinjilive.mobile.mall.shop.MineShopOrder;
import com.ggh.jinjilive.mobile.mall.shop.MineShopOrderTask;
import com.ggh.jinjilive.mobile.mall.shop.ShopMessageMobile;
import com.ggh.jinjilive.remotedata.InterceptObserver;
import com.ggh.jinjilive.remotedata.systemapi.SystemApiService;
import com.ggh.jinjilive.ui.BaseViewModel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class MineShopViewModel extends BaseViewModel {
    //获取订单
    public MutableLiveData<List<MineShopOrder>> MineShopOrderList = new MutableLiveData<>();
    //任务列表
    public MutableLiveData<List<MineShopOrderTask>> MineShopOrderTaskList = new MutableLiveData<>();
    //商品列表
    public MutableLiveData<List<MallGoods>> MallGoodsList = new MutableLiveData<>();
    //门店中心
    public MutableLiveData<ShopMessageMobile> mShopMessageMobile = new MutableLiveData<>();

    public MineShopViewModel(SystemApiService apiCallInterface) {
        super(apiCallInterface);

    }

    /**
     * 获取订单
     * @param status
     */
    public void orderList(int status){
        if (isRefresh) {
            page = 1;
        }else {
            page = page + 1;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("status", status);
        map.put("page",page);
        addSubscribe(apiCallInterface.shopOrderList(map).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<List<MineShopOrder>>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                List<MineShopOrder> mineShopOrderList = (List<MineShopOrder>) resultEntity.getData();
                MineShopOrderList.setValue(mineShopOrderList);
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {
                MineShopOrderList.setValue(null);
            }
        }));
    }

    /**
     * 任务列表
     */
    public void taskList(){
        if (isRefresh) {
            page = 1;
        }else {
            page = page + 1;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("page",page);
        addSubscribe(apiCallInterface.shopTaskList(map).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<List<MineShopOrderTask>>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                List<MineShopOrderTask> mineShopOrderTaskList = (List<MineShopOrderTask>) resultEntity.getData();
                MineShopOrderTaskList.setValue(mineShopOrderTaskList);
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {
                MineShopOrderTaskList.setValue(null);
            }
        }));
    }

    /**
     * 商品列表
     * @param status
     */
    public void shopProductList(int status){
        if (isRefresh) {
            page = 1;
        }else {
            page = page + 1;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("page",page);
        map.put("status",status);
        addSubscribe(apiCallInterface.shopProductList(map).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<List<MallGoods>>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                List<MallGoods> mMallGoodsList = (List<MallGoods>) resultEntity.getData();
                MallGoodsList.setValue(mMallGoodsList);
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {
                MineShopOrderTaskList.setValue(null);
            }
        }));
    }


    /**
     *  门店中心
     */
    public void StoreStoreInfo(){
        addSubscribe(apiCallInterface.StoreStoreInfo().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<ShopMessageMobile>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                ShopMessageMobile shopMessageMobile = (ShopMessageMobile) resultEntity.getData();
                mShopMessageMobile.setValue(shopMessageMobile);
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {
                mShopMessageMobile.setValue(null);
            }
        }));
    }
}
